package com.qf.fcy.youshare.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.fcy.youshare.Constants;
import com.qf.fcy.youshare.R;
import com.qf.fcy.youshare.model.Product;
import com.qf.fcy.youshare.utils.FileCache;
import com.qf.fcy.youshare.utils.ImageCache;
import com.qf.fcy.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/7
 * Email: alex_fcy@163.com
 */
public class StorePlatformAdapter extends BaseAdapter {

    private List<Product> products;
    private LayoutInflater inflater;

    public StorePlatformAdapter(Context context, List<Product> products) {
        inflater = LayoutInflater.from(context);
        this.products = products;
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_store_platform,parent,false);
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.item_platform_icon);
            holder.txtTitle= (TextView) convertView.findViewById(R.id.item_platform_title);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.item_platform_price);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        Product product = products.get(position);
        holder.txtTitle.setText(product.getGoodsName());
        holder.txtPrice.setText(product.getShopPrice());

        String goodsThumb = product.getGoodsThumb();
        // 拼接图片网址
        String reqUrl = Constants.IMG_SERVER + "/" + goodsThumb;
        //TODO 加载图片
        if (goodsThumb != null && goodsThumb.length() > 0) {
            if (!goodsThumb.startsWith("http://") && !goodsThumb.startsWith("https://")) {//有效网址
                holder.imgIcon.setTag(reqUrl);

                // 进行内存缓存的检查，如果有图片，直接加载
                ImageCache imageCache = ImageCache.getInstance();
                Bitmap bitmap = imageCache.getImage(reqUrl);
                if (bitmap != null) {// 如果内存中包含图片对象 Bitmap 那么就直接设置 ImageView
                    holder.imgIcon.setImageBitmap(bitmap);
                } else { // 不包含，则检查本地文件是否存在缓存
                    //TODO 检查本地文件是否存在图片缓存
                    FileCache fileCache = FileCache.getInstance();
                    byte[] bytes = fileCache.getContent(reqUrl);
                    if (bytes != null && bytes.length > 0) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        holder.imgIcon.setImageBitmap(bmp);

                        imageCache.putImage(reqUrl, bmp);
                    } else {
                        // TODO 本地不存在图片缓存，则先设置默认图片，再开启异步任务下载图片
                        holder.imgIcon.setImageResource(R.drawable.product_default);
                        ImageLoader task = new ImageLoader(holder.imgIcon);
                        task.execute(reqUrl);
                    }
                }
            } else {
                Log.d("YouShare", "服务器图片网址错误");
            }
        }

        return convertView;
    }

    private static class ViewHolder {
        public TextView txtTitle;
        public TextView txtPrice;
        public ImageView imgIcon;
    }
}
